package com.langsin.StackDemo;

/**
 * @Auther: MFM
 * @Date: 2021/7/16 - 07 - 16 - 20:40
 * @version: 1.0
 */

public class ArrayStack<E> implements Stack<E> {

    //动态数组
    private Array<E> array;

    //带参构造函数
    public ArrayStack(int capacity) {
        array = new Array<>(capacity);
    }

    //无参构造函数
    public ArrayStack() {
        array = new Array<>();
    }

    //入栈 压栈
    @Override
    public void push(E e) {
        array.addLast(e);
    }

    //出栈
    @Override
    public E pop() {
        return array.removeLast();
    }

    //查看栈顶元素
    @Override
    public E peek() {
        return array.get(getSize() - 1);
    }

    //查看栈有多少元素
    @Override
    public int getSize() {
        return array.getSize();
    }

    //查看栈是否为空
    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    //返回栈的容量
    public int getCapacity() {
        return array.getCapacity();
    }

    @Override
    public String toString() {
        return "Stack" + array + "栈顶";
    }
}
